export default {
    namespaced: true,
    state: {//仓库
        all:[],
        site:null,
        user:null,
        consignee:null,
        phone:null,
        siteId:null,
        cartId:[],//存储进入订单页的购物车商品的id
        product:{
            prices:null,
            quantity:0,
            item:[],
            // site:[],
        },
        //需要删除的购物车数据
        cart:null
    },
    //计算
    getters: {

    },
    //更改仓库
    mutations: {
        init(state, payload){
            state.cart = payload;//选中的商品传到订单
            state.product.prices = 0;
            state.product.quantity = 0;
            state.product.item = [];
            state.cartId = [];//过滤之前初始化
            payload.filter((item)=>{
                state.product.item.push(item);
                state.cartId.push(item.id);
                state.product.prices +=  (item.num * item.price);
                state.product.quantity += item.num;
            });
        },
        _site(state, payload){
            if(payload.length){
                state.all = payload;
                payload = payload.filter((item)=>{
                    return item.activate === 1;
                });
                payload = payload[0];
                state.site =  payload.site;
                state.siteId =  payload.id;
                state.user =  payload.user;
                state.consignee = payload.consignee;
                state.phone = payload.phone;
            }
        }
    },
    //异步请求
    actions: {
        add({commit,rootState: {$http}},payload){
            $http.post("/order/add",{id:payload.id})
                .then(result => commit("_add", payload));
        },
        site({commit,rootState: {$http}},payload){
            $http.post("/order/site",{user:payload})
                .then(result => {
                    commit("_site", result.data)
                });
        },
        del({commit,rootState: {$http}},orderId){
            $http.post("/order/delOrderDetail",{id:orderId})
                .then(result => {
                   console.log(result);
                });
        },
        payment({commit,rootState: {$http}},orderId){
            $http.post("/order/payment",{id:orderId})
                .then(result => {
                    console.log(result);
                });
        }
    }
};